package bigo;

import bigo.data.MatrixMeta;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileContext;
import org.apache.hadoop.fs.Options.Rename;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapred.LocalJobRunner;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 *
 * @author songliu
 */
public class SVMTrainingDriver extends Configured implements Tool {

    public int run(String[] args) throws Exception {
        String trainingSet = args[0], trainingSet_T = trainingSet + "_T";
        int m = Integer.parseInt(args[1]);
        int n = Integer.parseInt(args[2]);
        int res = 0;
        //make the kernel
//        res = ToolRunner.run(getConf(), new MultiplicationDriver(),
//                new String[]{trainingSet, trainingSet_T, "kernel",
//                    "" + m, "" + n, "" + m,
//                    "5", "4", "5"});
//        if (res != 0) {
//            return res;
//        }

        for (int i = 0; i < 100; i++) {
            System.out.println(i);
            float eta = (float) (0.2 / Math.sqrt(i + 2.0));
            getConf().setFloat("eta.rate", eta);
            System.out.println(eta);

            res = ToolRunner.run(getConf(),
                    new LargeASmallBMultiplicationDriver(
                    new MatrixMeta("kernel", m, m),
                    new MatrixMeta("alpha", m, 1),
                    new MatrixMeta("z", m, 1)), args);

            if (res != 0) {
                return res;
            }

            res = ToolRunner.run(getConf(),
                    new SVMAggregationDriver(
                    new MatrixMeta("eta", m, 1),
                    new MatrixMeta("y", m, 1),
                    new MatrixMeta("z", m, 1),
                    new MatrixMeta("alpha", m, 1),
                    new MatrixMeta("output", m, 1)),
                    args);

            if (res != 0) {
                return res;
            }

            FileContext context = FileContext.getFileContext(getConf());
            context.delete(new Path("alpha"), true);
            context.rename(new Path("output"), new Path("alpha"), Rename.OVERWRITE);
        }
        return res;
    }

    public static void main(String[] args) throws Exception {
        ToolRunner.run(new Configuration(), new SVMTrainingDriver(), args);
    }
}
